﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;

namespace AsteroidWar
{
    class SecondPlayer
    {
        public int dame;
        public Sprite playerSprite;
        public float playerSpeed = 220.0f;
        private Rectangle playerAreaLimit;
        public long PlayerScore = 0;
        public int LiveRemaining = 3;
        public bool Destroyed = false;

        private Vector2 gunOffset = new Vector2(23, 10);
        private float shotTime = 0.0f;
        private float minShotTimer = 0.2f;
        private int playerRadius = 25;
        private List<Enemy> enemies;
        public ShotManager PlayerShotManager;
        public bool firstDead = true;
        public float deadTime = 0f;
        public SecondPlayer(
            Texture2D playerTexture,
            Rectangle initialFrame,
            Texture2D shotTexture,
            Rectangle ammoFrame,
            int frameCount,
            Rectangle playerAreaLimit,
            Vector2 location
            )
        {
            this.playerAreaLimit = playerAreaLimit;

            playerSprite = new Sprite(location, playerTexture, initialFrame, Vector2.Zero);

            PlayerShotManager = new ShotManager(shotTexture, ammoFrame, 1, 13, 250f, playerAreaLimit);

            for (int x = 1; x < frameCount; x++)
            {
                playerSprite.AddFrame(new Rectangle(
                    initialFrame.X + (initialFrame.Width * x),
                    initialFrame.Y,
                    initialFrame.Width,
                    initialFrame.Height));
            }
            playerSprite.CollisionRadius = playerRadius;
        }

        public void FireShot()
        {
            if (shotTime >= minShotTimer) //adjust delay time between 2 continuos shots
            {
                PlayerShotManager.FireShot(playerSprite.Location + gunOffset, new Vector2(0, -1), true);
                shotTime = 0.0f;
            }
        }

        public void setListEnemies(List<Enemy> enemies)
        {
            this.enemies = enemies;
        }

        public void autoShot()
        {         
            foreach (Enemy enemy in enemies)
            {
                if (Math.Abs(enemy.sprite.Center.X - playerSprite.Center.X ) < 200 &&
                    playerSprite.Center.Y > enemy.sprite.Center.Y + 80)
                {
                    FireShot();
                }
            }         
        }

        public void HandleKeyboardInput(KeyboardState keyState)
        {
            if (keyState.IsKeyDown(Keys.F))
            {
                FireShot();
            }
           

            if (keyState.IsKeyDown(Keys.A))
            {
                playerSprite.Velocity += new Vector2(-1, 0);
            }
            if (keyState.IsKeyDown(Keys.D))
            {
                playerSprite.Velocity += new Vector2(1, 0);
            }
            if (keyState.IsKeyDown(Keys.W))
            {
                playerSprite.Velocity += new Vector2(0, -1);
            }
            if (keyState.IsKeyDown(Keys.S))
            {
                playerSprite.Velocity += new Vector2(0, 1);
            }
        }

        public void adjustPlayerLocation()
        {
            Rectangle boundingBox = playerSprite.Destination;
            Vector2 location = playerSprite.Location;

            if (boundingBox.Left < playerAreaLimit.Left)
            {
                location.X = playerAreaLimit.Left;
            }

            if (boundingBox.Right > playerAreaLimit.Right)
            {
                location.X = playerAreaLimit.Right - boundingBox.Width;
            }

            if (boundingBox.Top < playerAreaLimit.Top)
            {
                location.Y = playerAreaLimit.Top;
            }

            if (boundingBox.Bottom > playerAreaLimit.Bottom)
            {
                location.Y = playerAreaLimit.Bottom - boundingBox.Height;
            }

            playerSprite.Location = location;
        }

        public void Update(GameTime gameTime)
        {
            PlayerShotManager.Update(gameTime);

            if (!Destroyed)
            {

                playerSprite.Velocity = Vector2.Zero;
                HandleKeyboardInput(Keyboard.GetState());

                shotTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
                playerSprite.Velocity *= playerSpeed;

                playerSprite.Update(gameTime);
                adjustPlayerLocation();
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            PlayerShotManager.Draw(spriteBatch);

            if (!Destroyed)
            playerSprite.Draw(spriteBatch, 1.0f);
        }
    }
}
